/**
 * date:2021/02/27
 * author:LeapMary
 * version:2.0
 * description:js常用扩展
 */
layui.define(["element", "layer", "jquery"], function (exports) {

    var element = layui.element,
        layer = layui.layer,
        $ = layui.$;

    var allJs = {
        /**
         * 全能ajax
         * 拓展，增删改查
         */
        ajaxAlmighty: function ($parameter) {

            var $data = {
                "model":$parameter['$model'],
                "where_arr":$parameter['$where_arr'],
                "up_arr":$parameter['$up_arr'],
            };

            var $func_index = function(data){
                layer.msg(data.msg);
                allJs.close();
                //关闭父级页面的表格//currentTableId为表格id
                if(undefined == parent.layui.table){
                    layui.table.reload('currentTableId');
                }else{
                    parent.layui.table.reload('currentTableId');
                }
            };

            if($parameter['$func_s'] != undefined){
                allJs.MDSAjax("/system/admin/common/update",$data,$func_s);
            }else{
                allJs.MDSAjax("/system/admin/common/update",$data,$func_index);
            }

        },
        /**
         * 万能ajax
         */
        ajaxUniversal: function ($model,$id_field, $id_val,$up_arr,$type = "index",$func_s = '') {

            var $data = {
                "model":$model,
                "id_field":$id_field,
                "id_val":$id_val,
                "up_arr":$up_arr,
            };

            var $func_index = function(data){
                layer.msg(data.msg);
                allJs.close();
                //关闭父级页面的表格//currentTableId为表格id
                if(undefined == parent.layui.table){
                    layui.table.reload('currentTableId');
                }else{
                    parent.layui.table.reload('currentTableId');
                }
            };

            if($func_s != ''){
                allJs.MDSAjax("/system/admin/common/update",$data,$func_s);
            }else{
                allJs.MDSAjax("/system/admin/common/update",$data,$func_index);
            }

        },
        /**
         * ajax
         */
        MDSAjax: function($url,$data,$successFunc){
            //万能修改接口
            $.ajax({
                type: "POST",
                url: $url,
                timeout: 777000, //超时时间设置，单位毫秒 为一秒
                data: $data,
                dataType: "json",      //如果有此选项成功返回的json会自动变成对象，如果没有需要用JSON.parse()转换
                success: function (data) {
                    $successFunc(data);
                }
            });
        },
        /**
         * 关闭弹窗
         */
        close:function () {
            var index = parent.layer.getFrameIndex(window.name);  //获取窗口索引
            parent.layer.close(index);
        },
        /**
         * ajax
         */
        verificationPhone: function($phone){
            if(!(/^1(2|3|4|5|6|7|8|9)\d{9}$/.test($phone))){
                return false;
            }else{
                return true;
            }
        },
    };

    exports("allJs", allJs);
});
